package com.smxy.daydayup.controller;

import com.smxy.daydayup.pojo.Question;
import com.smxy.daydayup.pojo.QuestionSort;
import com.smxy.daydayup.pojo.ao.QuestionAo;
import com.smxy.daydayup.service.QuestionService;
import com.smxy.daydayup.service.QuestionSortService;
import com.smxy.daydayup.util.datetime.DateUtil;
import com.smxy.daydayup.util.result.ResultUtil;
import com.smxy.daydayup.util.result.ReturnResult;
import com.smxy.daydayup.util.result.StatusCode;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @team daydayup
 * @time 2021-12-13 15:31
 */

@Api(tags = "题目控制器")
@RestController
@RequestMapping("/question")
public class QuestionController {

    @Autowired
    QuestionService questionService;
    @Autowired
    QuestionSortService questionSortService;

    @ApiOperation("展示题目分类的接口")
    @GetMapping("/sort/show")
    public ReturnResult showSort() {
        return ResultUtil.returnSuccess(questionSortService.selectAll(), "success");
    }

    @ApiOperation("增加题目分类的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "questionSortName",
                    value = "问题分类名",
                    required = true,
                    dataTypeClass = String.class
            ),
            @ApiImplicitParam(
                    name = "isOption",
                    value = "是否有选项的标识",
                    required = true,
                    dataTypeClass = Integer.class
            )
    })
    @PostMapping("/sort/add")
    public ReturnResult addSort(@RequestParam("questionSortName") String questionSortName,
                                @RequestParam("isOption") Integer isOption) {
        questionSortService.insertSelective(new QuestionSort(null, questionSortName, isOption, DateUtil.getNowDate(), DateUtil.getNowDate()));
        return ResultUtil.returnSuccess();
    }

    @ApiOperation("添加题目的接口")
    @PostMapping("/add")
    public ReturnResult addQuestion(@ApiParam("前端问题信息实体类") @RequestBody QuestionAo questionAo) {
        if (questionService.createQuestion(questionAo) > 0) {
            return ResultUtil.returnSuccess();
        } else {
            return ResultUtil.returnFail(StatusCode.SERVER_EXCEPTION, "服务器异常");
        }
    }

    @ApiOperation("逻辑删除题目的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "questionId",
                    value = "问题id",
                    required = true,
                    dataTypeClass = Long.class
            ),
            @ApiImplicitParam(
                    name = "flag",
                    value = "逻辑删除标识",
                    required = false,
                    dataTypeClass = boolean.class
            )
    })
    @PostMapping("/delete/logic")
    public ReturnResult deleteQuestionByLogic(@RequestParam("questionId") Long questionId,
                                              @RequestParam(value = "flag", required = false) boolean flag) {
        // flag为true逻辑上删除题目，flag为false或者为空时恢复题目
        questionService.deleteByLogic(questionId, flag);
        return ResultUtil.returnSuccess();
    }

    @ApiOperation("展示题库的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "courseId",
                    value = "课程id",
                    required = true,
                    dataTypeClass = Long.class
            ),
            @ApiImplicitParam(
                    name = "isDelete",
                    value = "逻辑删除标识",
                    required = false,
                    dataTypeClass = Integer.class
            )
    })
    @GetMapping("/show")
    public ReturnResult showBank(@RequestParam("courseId") Long courseId,
                                 @RequestParam(value = "isDelete", required = false) Integer isDelete) {
        return ResultUtil.returnSuccess(questionService.selectQuestionList(courseId, isDelete), "success");
    }

    @ApiOperation("展示对应分类题库的接口")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    name = "courseId",
                    value = "课程id",
                    required = true,
                    dataTypeClass = Long.class
            ),
            @ApiImplicitParam(
                    name = "questionId",
                    value = "问题id",
                    required = true,
                    dataTypeClass = Long.class
            ),
            @ApiImplicitParam(
                    name = "isDelete",
                    value = "逻辑删除标识",
                    required = true,
                    dataTypeClass = Integer.class
            )
    })
    @GetMapping("/show/sort")
    public ReturnResult showBank(@RequestParam("courseId") Long courseId,
                                 @RequestParam("questionSortId") Long questionsSortId,
                                 @RequestParam(value = "isDelete", required = false) Integer isDelete) {
        return ResultUtil.returnSuccess(questionService.selectQuestionListBySort(courseId, questionsSortId, isDelete), "success");
    }

    @ApiOperation("展示对应题目内容的接口")
    @ApiImplicitParam(
            name = "questionId",
            value = "问题id",
            required = true,
            dataTypeClass = Long.class
    )
    @GetMapping("/details")
    public ReturnResult showQuestion(@RequestParam("questionId") Long questionId) {
        return ResultUtil.returnSuccess(questionService.selectByPrimaryKey(questionId), "success");
    }

    @ApiOperation("删除题目的接口")
    @ApiImplicitParam(
            name = "questionId",
            value = "问题id",
            required = true,
            dataTypeClass = Long.class
    )
    @PostMapping("/delete")
    public ReturnResult deleteQuestion(@RequestParam("questionId") Long questionId) {
        questionService.deleteByPrimaryKey(questionId);
        return ResultUtil.returnSuccess();
    }
}
